//
//  QLPublic.m
//  QLSchool_IOS
//
//  Created by anan on 14-1-10.
//  Copyright (c) 2014年 baobaodev_anan. All rights reserved.
//

#import "QLPublic.h"

/**
 *  麒麟公用模块
 */
@implementation QLPublic

+ (NSString *)getVersionString
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return app_Version;
}

+ (int)getVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    int app_Version = (int)[[infoDictionary objectForKey:@"CFBundleVersion"] integerValue];
    return app_Version;
}

+ (NSString *)addParam2UrlStr:(NSString*)url key:(NSString*)key value:(NSString*)value
{
    if (url!=nil&&url.length>0) {
        NSRange range = [url rangeOfString:@"?"];
        if (range.location==NSNotFound) {
            url = [NSString stringWithFormat:@"%@?%@=%@",url,key,value];
        }else{
            url = [NSString stringWithFormat:@"%@&%@=%@",url,key,value];
        }
    }
    return url;
}

+ (NSString *)addParam2ImageUrlStr:(NSString*)url w:(int)w h:(int)h m:(int)m
{
    if (url!=nil) {
        if (w>0) {
            url = [self addParam2UrlStr:url key:@"w" value:[NSString stringWithFormat:@"%d",w*2]];
        }
        if (h>0) {
            url = [self addParam2UrlStr:url key:@"h" value:[NSString stringWithFormat:@"%d",h*2]];
        }
        if (m>0) {
            url = [self addParam2UrlStr:url key:@"m" value:[NSString stringWithFormat:@"%d",m]];
        }
    }
    return url;
}

+ (NSString *)addParam2ImageUrlStr:(NSString*)url w:(int)w h:(int)h
{
    return [self addParam2ImageUrlStr:url w:w h:h m:-1];
}

+ (NSString *)addParam2ImageUrlStr:(NSString*)url w:(int)w
{
    return [self addParam2ImageUrlStr:url w:w h:-1 m:-1];
}

+ (BOOL)isWXAppInstalled
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]])
    {
        return YES;
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"wechat://"]])
    {
        return YES;
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb290293790992170://"]])
    {
        return YES;
    }
    return NO;
}

+ (UIViewController *)getCurrentRootViewController
{
    
    UIViewController *result;
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    if (topWindow.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(topWindow in windows)
        {
            if (topWindow.windowLevel == UIWindowLevelNormal)
                break;
        }
    }
    
    UIView *rootView = [[topWindow subviews] objectAtIndex:0];
    id nextResponder = [rootView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil)
        result = topWindow.rootViewController;
    else
        NSAssert(NO, @"ShareKit: Could not find a root view controller.  You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
    
    return result;
}

+ (void)setScrollsToTopWithContentView:(UIView*)contentView enable:(BOOL)enable
{
    for (UIScrollView *view in [contentView subviews]) {
        if ([view isMemberOfClass:[UIScrollView class]]||[view isMemberOfClass:[UITableView class]]||[view isMemberOfClass:[UICollectionView class]]||[view isMemberOfClass:[PullCollectionView class]]) {
            [view setScrollsToTop:enable];
        }
    }
}

/**
 *  获取MAC
 *
 *  @return mac字符串
 */
+ (NSString *) macaddress
{
    int                 mib[6];
    size_t              len;
    char                *buf;
    unsigned char       *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl  *sdl;
    
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1/n");
        return NULL;
    }
    
    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }
    
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    
    //    NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    
    NSLog(@"outString:%@", [outstring uppercaseString]);
    
    free(buf);
    
    return [outstring uppercaseString];
}

+ (BOOL)isSuccess:(NSDictionary*)dic
{
    if (dic&&[dic ql_hasObjectForKey:@"MESSAGE_INFO"]) {
        NSDictionary *info = [dic objectForKey:@"MESSAGE_INFO"];
        if ([info ql_hasObjectForKey:@"CODE"]&&[[info ql_stringForKey:@"CODE"] isEqualToString:@"0000"]) {
            return YES;
        }
    }
    return NO;
}

+ (BOOL)isAdminAccount
{
    NSDictionary *ui = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    NSString *account = [ui ql_stringForKey:@"LOGIN_NAME"];
    if ([account isEqualToString:@"18688859030"]||[account isEqualToString:@"15933333317"]||[account isEqualToString:@"15913521595"]) {
        return YES;
    }else{
        return NO;
    }
}

@end
